package com.android.ddfafu.ui.gallery;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.ddfafu.MapApplication;
import com.android.ddfafu.R;
import com.android.ddfafu.activity.Image_activity;
import com.android.ddfafu.domian.ImageLab;
import java.util.List;


public class GalleryFragment extends Fragment {
    private MapApplication application;
    private SharedPreferences sp;
    private RecyclerView mRecyclerView;
    private ImageAdapter mAdapter;


    private GalleryViewModel galleryViewModel;
    private List<Bitmap> bitmapList;
    private List<String> bitmapPathList;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                ViewModelProviders.of(this).get(GalleryViewModel.class);
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);

        mRecyclerView = (RecyclerView) root.findViewById(R.id.album_wall_recycler);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3,LinearLayoutManager.VERTICAL,false));

        //updateUI();

        return root;

    }

    @Override
    public void onResume() {
        Log.i("onResume","onResume");
        updateUI();
        super.onResume();


    }

    @Override
    public void onPause() {
        Log.i("onPause","onPause");
        super.onPause();
    }

    private void updateUI() {
        ImageLab imageLab = ImageLab.get(getActivity());
        application = (MapApplication)getActivity().getApplication();
        bitmapList = application.getBitmapList();
        bitmapPathList = application.getBitmapPath();

        mAdapter = new ImageAdapter(bitmapList,bitmapPathList);
        //设置Adapter
        mRecyclerView.setAdapter(mAdapter);
    }

    private class ImageHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {

        private Bitmap mBitmap;

        private ImageView mImageView;


        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        public ImageHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.image_list, parent, false));
            //itemView.setId(View.generateViewId());
            itemView.setOnClickListener(this);
            mImageView = (ImageView) itemView.findViewById(R.id.img_list);
            mImageView.setId(View.generateViewId());
            mImageView.setOnClickListener(v -> {
                Intent intent = new Intent(getActivity(),Image_activity.class);
                Bitmap bm =((BitmapDrawable) mImageView.getDrawable()).getBitmap();
                System.out.println(bm.getByteCount());
                String path = (String)mImageView.getTag();
                System.out.println(path);
                application.setmBitmap(bm);
                intent.putExtra("path",path);
                startActivity(intent);

            });

        }

        public void bind(Bitmap bitmap,String path) {
            try {
                mBitmap = bitmap;
                mImageView.setImageBitmap(bitmap);
                mImageView.setTag(path);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        @Override
        public void onClick(View view) {
            // 放大图片
            int id = view.getId();
            Intent intent = new Intent(getActivity(), Image_activity.class);
            startActivity(intent);
            Toast.makeText(getActivity(),
                    "image clicked!", Toast.LENGTH_SHORT)
                    .show();
        }
    }

    private class ImageAdapter extends RecyclerView.Adapter<ImageHolder> {

        private List<Bitmap> mBitmaps;
        private List<String> mBitmapPaths;

        public ImageAdapter(List<Bitmap> bitmaps,List<String> bitmapPaths) {
            mBitmaps = bitmaps;
            mBitmapPaths = bitmapPaths;
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            return new ImageHolder(layoutInflater, parent);
        }

        @Override
        public void onBindViewHolder(ImageHolder holder, int position) {
            Bitmap bitmap = mBitmaps.get(position);
            String path = mBitmapPaths.get(position);
            holder.bind(bitmap,path);
        }

        @Override
        public int getItemCount() {
            return mBitmaps.size();
        }
    }

}